home *** CD-ROM | disk | FTP | other *** search
/ Freelog 22 / freelog 22.iso / Prog / Djgpp / GPC2952B.ZIP / doc / gpc / demos / initvardemo.pas < prev    next >
Encoding:
Pascal/Delphi Source File  |  2001-02-08  |  3.5 KB  |  97 lines

  1. {
  2. GPC demo program about initialized variables and typed constants.
  3.  
  4. Copyright (C) 1999-2001 Free Software Foundation, Inc.
  5.  
  6. Author: Frank Heckenbach <frank@pascal.gnu.de>
  7.  
  8. This program is free software; you can redistribute it and/or
  9. modify it under the terms of the GNU General Public License as
  10. published by the Free Software Foundation, version 2.
  11.  
  12. This program is distributed in the hope that it will be useful,
  13. but WITHOUT ANY WARRANTY; without even the implied warranty of
  14. MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  15. GNU General Public License for more details.
  16.  
  17. You should have received a copy of the GNU General Public License
  18. along with this program; see the file COPYING. If not, write to
  19. the Free Software Foundation, Inc., 59 Temple Place - Suite 330,
  20. Boston, MA 02111-1307, USA.
  21.  
  22. As a special exception, if you incorporate even large parts of the
  23. code of this demo program into another program with substantially
  24. different functionality, this does not cause the other program to
  25. be covered by the GNU General Public License. This exception does
  26. not however invalidate any other reasons why it might be covered
  27. by the GNU General Public License.
  28. }
  29.  
  30. program InitVarDemo;
  31.  
  32. var
  33.   Foo : Integer value 42;  { An initialized variable according to ISO 10206 }
  34.   Bar : Integer = 17;      { GPC also supports this syntax }
  35.  
  36. type
  37.   TBaz = Integer value 19; { A type with an initialization }
  38.  
  39. var
  40.   Baz : TBaz;              { A variable initialized from its type }
  41.  
  42. const
  43.   Qux : Integer = 22;      { A typed *constant* }
  44.  
  45. procedure DemoGlobal;
  46. begin
  47.   Writeln ('Initial values:             Foo = ', Foo, '  Bar = ', Bar, '  Baz = ', Baz, '  Qux = ', Qux);
  48.   Inc (Foo);
  49.   Dec (Bar);
  50.   Baz := 2 * Baz;
  51.   Qux := 0; { A constant cannot be modified. GPC gives only a warning
  52.               because many programs written for BP or similar
  53.               compilers rely on the unfortunate behaviour of these
  54.               compilers. Still, one should rather use an initialized
  55.               variable when one wants a variable, and a typed
  56.               constant only for real constants. }
  57.   Writeln ('Values after modification:  Foo = ', Foo, '  Bar = ', Bar, '  Baz = ', Baz, '  Qux = ', Qux);
  58.   Writeln
  59. end;
  60.  
  61. procedure DemoLocal;
  62. var
  63.   Foo : Integer = 42;        { An initialized local variable. It is
  64.                                initialized whenever the procedure is
  65.                                entered. }
  66.  
  67.   Bar : static Integer = 17; { A static initialized variable. It is only
  68.                                initialized once at the beginning of the
  69.                                program. `static' is a GNU Pascal extension. }
  70.  
  71. const
  72.   Baz : Integer = 19;        { A typed constant. Some people use them instead
  73.                                of static variables, because some compilers
  74.                                that don't know `static' automatically make
  75.                                typed constants static. GPC emulates this
  76.                                behaviour, but it is not recommended to make
  77.                                use of it. Better declare your variables
  78.                                `static' when that's what you mean. }
  79.  
  80. begin
  81.   Writeln ('Initial values:             Foo = ', Foo, '  Bar = ', Bar, '  Baz = ', Baz);
  82.   Inc (Foo);
  83.   Dec (Bar);
  84.   Baz := 2 * Baz; { Again, not recommended }
  85.   Writeln ('Values after modification:  Foo = ', Foo, '  Bar = ', Bar, '  Baz = ', Baz);
  86.   Writeln
  87. end;
  88.  
  89. begin
  90.   Writeln ('Global variables:');
  91.   DemoGlobal;
  92.   Writeln ('Local variables, first run:');
  93.   DemoLocal;
  94.   Writeln ('Local variables, second run:');
  95.   DemoLocal
  96. end.
  97.